package db

import (
	"github/casperkwok/shortener/internal/model"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func InitDB(dbPath string) (*gorm.DB, error) {

	db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
	if err != nil {
		return nil, err
	}

	// 自动迁移表
	if err := db.AutoMigrate(&model.URL{}); err != nil {
		return nil, err
	}

	return db, nil

}
